package com.qingyu.print.member.service.impl;

import com.qingyu.print.member.utils.DataTimeUtil;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingyu.common.utils.PageUtils;
import com.qingyu.common.utils.Query;

import com.qingyu.print.member.dao.UserDao;
import com.qingyu.print.member.entity.UserEntity;
import com.qingyu.print.member.service.UserService;

import javax.annotation.Resource;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Resource
    private UserDao userMapper;

    @Override
    public void saveU(UserEntity user) throws Exception {
        if (userMapper.selectOne(new QueryWrapper<UserEntity>().in("phone", user.getPhone())) != null)
            throw new Exception("当前手机号码已注册");
        if (userMapper.selectOne(new QueryWrapper<UserEntity>().in("nickname", user.getNickname())) != null)
            throw new Exception("该昵称已存在");
        user.setCreateAt(DataTimeUtil.getNowTimeString());
        user.setId(UUID.randomUUID().toString());
        userMapper.insert(user);

    }

    @Override
    public UserEntity login(UserEntity user) throws Exception {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.in("phone", user.getPhone());
        wrapper.in("password", user.getPassword());
        UserEntity one = userMapper.selectOne(wrapper);
        if (one == null) throw new Exception("账号密码错误");
        return one;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

}